package com.frxuexi.mdm2204.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.mdm2204.R

data class HomeEntity(val title:String,val img:Int)
@Composable
fun HomePage(navHostController: NavHostController) {
    val list = listOf<HomeEntity>(
        HomeEntity("在线应用", R.drawable.icon32),
        HomeEntity("文件", R.drawable.icon3),
        HomeEntity("工作日志", R.drawable.icon359),
        HomeEntity("考勤打卡", R.drawable.icon1912),
        HomeEntity("智能审批", R.drawable.icon81),
        HomeEntity("任务", R.drawable.icon105)

    )

    val list2 = listOf<HomeEntity>(
        HomeEntity("联系人", R.drawable.icon1887),
        HomeEntity("讨论组", R.drawable.icon980),
        HomeEntity("组织架构", R.drawable.icon1907)
    )

    Column {
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(14.dp), verticalAlignment = Alignment.CenterVertically) {
            Text(text = "12", color = Color.White, modifier = Modifier
                .background(colorResource(id = R.color.pink), CircleShape)
                .padding(6.dp))
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "主门户", fontSize = 18.sp)
        }
        TitleItem(entity = HomeEntity("应用",R.drawable.icon814))
        LazyVerticalGrid(columns = GridCells.Fixed(3), content = {
            items(list){
                ContentItem(entity = it,navHostController)
            }
        })
        TitleItem(entity = HomeEntity("通信",R.drawable.icon1488))
        LazyVerticalGrid(columns = GridCells.Fixed(3), content = {
            items(list2){
                ContentItem(entity = it,navHostController)
            }
        })
        TitleItem(entity = HomeEntity("公众号",R.drawable.icon1488))
    }
}

@Composable
fun TitleItem(entity: HomeEntity){
    Row (Modifier.padding(10.dp), verticalAlignment = Alignment.CenterVertically){
        Image(painter = painterResource(id = entity.img), contentDescription = "")
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = entity.title)
    }
}

@Composable
fun ContentItem(entity: HomeEntity,navHostController:NavHostController){
    Column (Modifier.padding(10.dp).clickable {
        if(entity.title=="文件") {
            navHostController.navigate(RouterPath.DOC_PAGE)
        }else if(entity.title=="在线应用"){
            navHostController.navigate(RouterPath.APPS_PAGE)
        }else if(entity.title=="工作日志"){
            navHostController.navigate(RouterPath.REPORT_PAGE)
        }else if(entity.title=="考勤打卡"){
            navHostController.navigate(RouterPath.SIGN_PAGE)
        }
    }, horizontalAlignment = Alignment.CenterHorizontally){
        Image(painter = painterResource(id = entity.img), contentDescription = "",
            Modifier.size(50.dp))
        Spacer(modifier = Modifier.height(5.dp))
        Text(text = entity.title)
    }
}